<template>
	<a-config-provider :locale="locale">
		<div id="app">
			<BasicLayout />
			<!-- 全局登录过期弹窗 -->
			<LoginExpiredModal
				v-model:visible="authStore.loginExpiredModalVisible"
				:redirectUrl="authStore.redirectUrl"
				@cancel="authStore.hideLoginExpiredModal"
			/>
		</div>
	</a-config-provider>
</template>

<script setup lang="ts">
import BasicLayout from '@/layout/BasicLayout.vue'
import LoginExpiredModal from '@/components/modal/LoginExpiredModal.vue'
import { useAuthStore } from '@/stores/useAuthStore'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { onMounted } from 'vue'
import zhCN from 'ant-design-vue/es/locale/zh_CN'

const locale = zhCN
const authStore = useAuthStore()
const loginUserStore = useLoginUserStore()

// 应用启动时初始化登录状态
onMounted(async () => {
	// 如果本地存储有用户信息且不是默认状态，验证会话是否有效
	if (loginUserStore.loginUser?.id && loginUserStore.loginUser.userName !== '未登录') {
		try {
			// 验证当前会话是否仍然有效
			await loginUserStore.fetchLoginUser()
		} catch (error) {
			// 会话已过期，清除本地存储的用户信息
			console.log('会话已过期，清除本地用户信息')
			loginUserStore.clearLoginUser()
		}
	}
})
</script>

<style scoped lang="scss"></style>
